//
//  NSString+JwCate.m
//  JwPack
//
//  Created by 陈警卫 on 2024-07-17.
//

#import "NSString+JwCate.h"
#import <UIKit/UIKit.h>

@implementation NSString (JwCate)

/// 去除前后空格
- (NSString *)jw_trimSpace {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

/// 去除前后换行
- (NSString *)jw_trimLine {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
}

/// 去除前后空格和换行
- (NSString *)jw_trimSpaceAndLines {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

/// 去除所有空格
- (NSString *)jw_trimAllSpace {
    return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}

/// 去除所有换行
- (NSString *)jw_trimAllLines {
    return [self stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}

/// 去除所有空格和换行
- (NSString *)jw_trimAllSpaceAndLines {
    return [[self jw_trimAllSpace] jw_trimAllLines];
}

/// 校验字符串
+ (NSString *)jw_stringValue:(id)value {
    if ([value isKindOfClass:[NSNumber class]]) {
        value = ((NSNumber *)value).stringValue;
    }
    if ([value isKindOfClass:[NSString class]]) {
        return value;
    }
    return @"";
}

/// 检查字符串是否为空
- (BOOL)jw_isBlank {
    NSString *string = self;
    if (!string) {
        return YES;
    }
    if (string.length == 0) {
        return YES;
    }
    if ([string isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

/// 检查字符串是否为空或只包含空白字符
- (BOOL)jw_isBlankChar {
    NSString *string = self;
    if (!string || [string length] == 0) {
        return YES;
    }
    NSCharacterSet *blank = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    for (NSInteger i = 0; i < string.length; ++i) {
        unichar c = [string characterAtIndex:i];
        if (![blank characterIsMember:c]) {
            return NO;
        }
    }
    return YES;
}

/// 复制
- (void)jw_copyPasteboard {
    [UIPasteboard generalPasteboard].string = self;
}

/// 转拼音
- (NSString *)jw_pinyinTon:(BOOL)ton {
    CFMutableStringRef value = (__bridge CFMutableStringRef)[self mutableCopy];
    CFStringTransform(value, NULL, kCFStringTransformToLatin, NO);
    if (ton) {
        CFStringTransform(value, NULL, kCFStringTransformStripDiacritics, NO);
    }
    return (__bridge_transfer NSString *)value;
}

/// 随机数
+ (NSString *)jw_randomString {
    NSInteger random = arc4random() % 1000000000;
    return [NSString stringWithFormat:@"%06ld", (long)random];
}


@end
